package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerMainOriginal;
import com.isunland.managesystem.entity.CustomerMainOriginal.rCustomerMain;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class CustomerListChooseFragment extends BaseListFragment {
  public final static String TAG = CustomerListFragment.class.getSimpleName();
  protected final static String EXTRA_CONTENT =
      CustomerListFragment.class.getSimpleName() + "EXTRA_CONTENT";
  protected final static String CUSTOMER_NAME = "customerName";
  protected final static String CUSTOMER_ID = "customerId";
  private static final int SEARCH_CUSTOMER = 0;
  private CustomerAdapter mAdapter;
  private String dataStatus = "publish";
  private String districtCode = "100";
  private String districtDesc = "中国";
  private String customerName = "";
  private ArrayList<rCustomerMain> mList;

  @Override public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.customer_namelist);
    mList = new ArrayList<>();
    mAdapter = new CustomerAdapter(mActivity, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setListAdapter(mAdapter);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu_add_search, menu);
    menu.getItem(0).setVisible(false).setEnabled(false);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:
        getActivity().finish();
        return true;

      //查询
      case R.id.menu_item_querys:

        Intent intent = new Intent(getActivity(), CustomerSearchActivity.class);
        intent.putExtra(CustomerSearchFragment.EXTRA_STATUS, dataStatus);
        intent.putExtra(CustomerSearchFragment.EXTRA_NAME, customerName);
        intent.putExtra(CustomerSearchFragment.EXTRA_AREACODE, districtCode);
        intent.putExtra(CustomerSearchFragment.EXTRA_AREANAME, districtDesc);
        startActivityForResult(intent, SEARCH_CUSTOMER);
        return true;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.URL_CUSTOMER_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("dataStatus", dataStatus);
    params.put("districtCode", districtCode);
    params.put("customerName", customerName);
    LogUtil.i(params.toString());
    return params;
  }

  @Override public void resolveResponse(String arg0) {

    mList.clear();
    mList.addAll(new Gson().fromJson(arg0, CustomerMainOriginal.class).getRows());
    mAdapter.notifyDataSetChanged();
  }

  public void onListItemClick(ListView l, View v, int position, long id) {
    if (position == 0) return;
    rCustomerMain item = mAdapter.getItem(position - 1);
    if (item == null) return;
    // 回传点击的客户id和名字
    String customerId = item.getId();
    String customerName = item.getCustomerName();
    Intent i = new Intent();
    i.putExtra(CUSTOMER_ID, customerId);
    i.putExtra(CUSTOMER_NAME, customerName);
    i.putExtra(EXTRA_CONTENT, item);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {

      return;
    }
    if (requestCode == SEARCH_CUSTOMER) {
      //			Intent i=new Intent();
      //			i.putExtra("districtCode", mDistrictCode);
      ////			i.putExtra("districtName", mDistrictName);
      //			i.putExtra("dataStatus", dataStaus);
      //			i.putExtra("customerName",etCustomerName.getText().toString());
      //			getActivity().setResult(Activity.RESULT_OK,i);

      districtCode = data.getStringExtra("districtCode");
      districtDesc = data.getStringExtra("districtDesc");
      dataStatus = data.getStringExtra("dataStatus");
      customerName = data.getStringExtra("customerName");
      volleyPost();
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}
